package car.manager;

import android.app.Application;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class BackStageManager extends Application {

	private SQLiteDatabase database;

	private enum profile_indexes {
		ID, NAME, CREDIT, EXPERIENCE
	}

	public void onCreate() {
		super.onCreate();

		DataBaseConnection dbc = new DataBaseConnection(this);

		database = dbc.getWritableDatabase();
		loadProfiles();
	}

	private void loadProfiles() {

		Cursor cursor = database.query(DataBaseConnection.PROFILE_TABLE, null,
				null, null, null, null, null, null);

		if (cursor.moveToFirst()) {
			getValues(cursor);
		}

	}

	private void getValues(Cursor cursor) {

		do {
			new UserProfile(cursor.getInt(profile_indexes.ID.ordinal()),
					cursor.getString(profile_indexes.NAME.ordinal()),
					cursor.getFloat(profile_indexes.CREDIT.ordinal()),
					cursor.getInt(profile_indexes.EXPERIENCE.ordinal()));

		} while (cursor.moveToNext());

	}

	public boolean addProfile(UserProfile up) {

		ContentValues values = new ContentValues();
		values.put(DataBaseConnection.PROFILE_ID, up.getId());
		values.put(DataBaseConnection.PROFILE_NAME, up.getName());
		values.put(DataBaseConnection.PROFILE_CREDIT, up.getMoney());
		values.put(DataBaseConnection.PROFILE_EXPERIENCE, up.getExperience());

		return database.insert(DataBaseConnection.PROFILE_TABLE, null, values) != 1 ? true
				: false;

	}
}
